package data.model;

import java.util.Iterator;
import java.util.TreeSet;


/**
 * Data structure for a processor
 * 
 * @author Will Home
 *
 */
public class Processor implements Comparable<Processor>{
	
	/** processor name	 */
	private String procName;
	
	/** processor description	 */
	private String procDesc;
	
	/**  */
	private double inactiveCost;
	
	/** */
	private double activeCost;
	
	/** set of processor action */
	private TreeSet<ProcAction> actSet;

	/** unique ID */
	private final int procID;

	/** ID generator */
	private static int idGen = 1;

	public Processor(String name)
	{
		procName = name;
		procID = idGen;
		actSet = new TreeSet<ProcAction>();
		idGen++;
	}

	//MUTATORS
	/**
	 * assign new processor name
	 * 
	 * @param name
	 */
	public void setProcName(String name)
	{
		procName = name;
	}

	/**
	 * Add processor description
	 * 
	 * @param desc
	 */
	public void setProcDesc(String desc)
	{
		procDesc = desc;
	}
	
	/**
	 * 
	 * 
	 * @param cost
	 */
	public void setInactiveCost(double cost)
	{
		inactiveCost = cost;
	}
	
	/**
	 * 
	 * @param cost
	 */
	public void setActiveCost(double cost)
	{
		activeCost = cost;
	}
	
	/**
	 * Add a processorAction
	 * 
	 * @param pAct
	 * @return success check
	 */
	public boolean addAction(ProcAction pAct)
	{
		return actSet.add(pAct);
	}
	
	/**
	 * Remove a processorAction
	 * 
	 * @param actRef
	 * @return success check
	 */
	public boolean removeAction(int actRef)
	{
		Iterator<ProcAction> it = actSet.iterator();

		while(it.hasNext())
		{
			if(it.next().getAction() == (actRef))
			{
				it.remove();
				return true;
			}
		}
		return false;
	}

	//ACCESSORS

	/**
	 *  
	 * @return processor name
	 */
	public String getProcName()
	{
		return procName;
	}
	
	/**
	 *  
	 * @return processor name with spaces removed
	 */
	public String getConcName()
	{
		return procName.replace(" ", "");
	}

	/**
	 * 
	 * @return processor description
	 */
	public String getProcDesc()
	{
		return procDesc;
	}

	/**
	 *  
	 * @return processor ID
	 */
	public int getProcID()
	{
		return procID;
	}
	
	/**
	 * 
	 * @return inactiveCost
	 */
	public double getInactiveCost()
	{
		return inactiveCost;
	}
	
	/**
	 * 
	 * @return activeCost
	 */
	public double getActiveCost()
	{
		return activeCost;
	}

	/**
	 * check whether this processor executes a given action and return the relevant ProcAction
	 * 
	 * @param actRef
	 * @return processor action
	 */
	public ProcAction getProcAction(int actRef)
	{
		ProcAction curr;
		Iterator<ProcAction> it = actSet.iterator();
		
		while(it.hasNext())
		{
			curr = it.next();
			if(curr.getAction() == actRef)
				return curr;
		}
		
		return null;
	}
	
	/**
	 * 
	 * @return Processor Actions associated with this Processor
	 */
	public TreeSet<ProcAction> getProcActions()
	{
		return actSet;
	}
	
	/**
	 * compares processors by processor name
	 * 
	 * @param other
	 * @return 0 if name is identical, -1 if lower, 1 if higher
	 */
	public int compareTo(Processor other)
	{
		return procName.compareTo(other.getProcName());
	}

}

